#ifndef __BLECOM__
#define __BLECOM__

#include <stdint.h>
#include <stdbool.h>
#include "ble_nus.h"
#include "prj_typedef.h"
#include "display_ctrl.h"
#include "ble_ancs_c.h"
#include "m_ancs_app.h"

// APP发起的所有指令的枚举
typedef enum
{
    SmartBand_NULL = 0x0,
    SmartBand_Set_Time = 0x01,
#ifdef no_camera_ui
#else
    SmartBand_Camera = 0x02, // ??
#endif
    SmartBand_Voltameter = 0x03,       // ??
    SmartBand_BLE_Central_Type = 0x04, //?????????,????????,??????
    SmartBand_BLE_MoveHand = 0x05,
    SmartBand_BLE_Nodisturb = 0x06,
    SmartBand_Get_Sport_Info = 0x07,
    SmartBand_BLE_HardReset = 0x08,            // �?重启指令
    SmartBand_BLE_AntiLostAlarm_Switch = 0x09, // 防丢报�?�开�?
    SmartBand_Set_TimeHourSystem = 0x0A,       // set/read time hour system, 1-byte .#0:24 #1:12
    SmartBand_Android_Breath_Packet = 0x0B,    // android breath packet,

    APP_CMD_GOTO_DFU = 0x0F,                   //�?动DFU
    APP_CMD_BAND_NOTIFY = 0x10,                //绑定通知
    APP_CMD_ANDROID_STOP_PHONE_CALL = 0x11,    //安卓挂断电话
    APP_CMD_SET_HOROLOGE_DISPLAY_ONOFF = 0X12, //设置�?腕亮�?
    APP_CMD_READ_EXERCISE = 0x13,              // 锻炼模式

    // 读取定时心率数据
    APP_CMD_HEART_AUTO_MEASURE_DATA = 0x15,

    // 开关定时心率功�?
    APP_CMD_HEART_AUTO_MEASURE_ENABLE = 0x16,
    // APP_CMD_GET_RES_TYPE = 0X17,         //�?定义界面类型读取
    APP_CMD_SET_DEVICE_NAME = 0x18,      //设置设�?�广�?名称
    APP_CMD_SET_WEATHER_CTRL = 0x19,     //天气开�?
    APP_CMD_SET_WEATHER_DATA = 0x1A,     //天气内�??
    APP_CMD_LCD_LIGHT_LEVEL_CTRL = 0x1B, //�?度控�?
    APP_CMD_MUSIC_ONOFF = 0X1C,
    APP_CMD_MUSIC_CONTROL = 0X1D,
    APP_CMD_SET_LCD_OFF_DELAY_TIME = 0X1F, //设置�?屏时�?
    APP_CMD_HR_CALIBRATION = 0x20,         //心率校准
    APP_CMD_SET_USER_CFG = 0x21,           //设置�?标参数和首页配置
    SmartBand_Set_Clock = 0x23,
    SmartBand_Get_Clock = 0x24,
    SmartBand_Set_SitLong = 0x25,
    SmartBand_Get_SitLong = 0x26,
    SmartBand_Set_Drink_Notify = 0x27,
    SmartBand_Get_Drink_Notify = 0x28,
    APP_CMD_SET_DISP_MODE = 0x29,      //�?竖屏设置
    APP_CMD_SET_HOME_PAGE_TYPE = 0X2A, //首页样式
    APP_CMD_SET_AGPS_CTRL = 0x30,
    SmartBand_Get_Sport_Detail = 0x43,
    APP_CMD_GET_SLEEP_DATA = 0x44,
    SmartBand_Get_Sport_Flash_Status = 0x46, // ??????
    SmartBand_Get_Now_Sport_Info = 0x48,     // ????????
    Smartband_LookForBand = 0x50,            //????
    Smartband_Set_Ancs = 0x60,
    Smartband_Get_Ancs = 0x61,
    SmartBand_Healthy_Info = 0x69,    //??????????
    SmartBand_Healthy_Getdata = 0x6A, //??????????
    Smartband_Get_HeartRate = 0x6B,   // ????????
    Smartband_start_ecg_measure = 0x6c,
    Smartband_ecg_upload = 0x6d,
    Smartband_ppg_upload = 0x6e,
    Smartband_lead_state_change = 0x6f,

    SmartBand_AndroidNotify = 0x72, // Android????
    // SmartBand_debug_sleep = 0x73,
    SmartBand_sports_state = 0x74,
    SmartBand_sea_level_pressure = 0x75,
#ifdef USE_PRESSURE
    SmartBand_calibration_pressure = 0x76,
#endif
#ifdef USE_BLE_METRONOME_HZ_SET
    SmartBand_set_metronome_hz = 0x77,
#endif
    SmartBand_get_device_type = 0x51,
    SmartBand_set_device_type = 0x52,
    SmartBand_clear_device_type = 0x53,
    SmartBand_get_show_device_name = 0x54,
    SmartBand_set_show_device_name = 0x55,
    SmartBand_clear_show_device_name = 0x56,
    SmartBand_get_device_name = 0x57,
    SmartBand_set_localize_code = 0x58,
#ifdef USE_CT1711
    SmartBand_get_temperature = 0x59,
    SmartBand_get_temperature_uint32 = 0x5a,
    SmartBand_measure_temperature = 0x5b,
    SmartBand_hyperthermia_notify = 0x5c,
    SmartBand_temperature_exit = 0x5d,
    SmartBand_temperature_interval = 0x5e,
#endif
#ifdef AXILLARY_TEMPERATURE
    SmartBand_axillary_measure = 0x5f,
#endif //#ifdef AXILLARY_TEMPERATURE
#ifdef NO_INFRARED_DETECTOR
#else
    SmartBand_ignore_infrared = 0x62,
#endif
    Smartband_ProDuctTest_Set_SN = 0X90,
    Smartband_ProDuctTest_Get_SN = 0X91,
    Smartband_ProDuctTest_STart_sensor = 0X92,
    Smartband_ProDuctTest_STart_button = 0x94,
    Smartband_ProDuctTest_STart_oled = 0X95,
    Smartband_ProDuctTest_STart_motor = 0X96,
    Smartband_ProDuctTest_STart_powerrecover = 0X97,
    Smartband_ProDuctTest_Wflag_pcb = 0X98,
    Smartband_ProDuctTest_Rflag_pcb = 0X99,
    Smartband_ProDuctTest_Wflag_FinalTest = 0X9a,
    Smartband_ProDuctTest_Rflag_FinalTest = 0X9b,
    Smartband_ProDuctTest_sleep = 0X9c,
    Smartband_ProDuctTest_set_PCBversion = 0x9d,
    Smartband_ProDuctTest_get_PCBversion = 0x9e,
    Smartband_ProDuctTest_get_softversion = 0x93,
    Smartband_ProDuctTest_STart_sensor_CP = 0x9f,

    //厂测指令
    PRODUCT_TEST_CMD_ASSEM_TEST = 0xB0, //生产测试指令

    // add developer test id
    SmartBand_Dt_Read_Data = 0xC0,      //读取任意地址的数�?
    APP_CMD_DT_WEAR_TEST,               //测试佩戴检�?
    APP_CMD_DT_STOP_ADV,                //关闭广播
    APP_CMD_DT_BLE_FAST_MODE = 0XC3,    //打开蓝牙�?速连接参数模�?
    APP_CMD_DT_DEBUG_INFO_CLEAR = 0XC4, // debug信息清除
    APP_CMD_DT_SENSOR_TEST = 0xC5,      //心率&gsensor数据透传测试开�?
    APP_CMD_DT_PRODUCT_RESET = 0XC6,    //手环产品复位，包�?清除产测标�?�等
    APP_CMD_DT_GSENSOR_TEST = 0XC7,     // gsensor通信测试
    APP_CMD_DT_HR_STEP_TEST = 0XC8,     //心率步�?�发�?
    APP_CMD_DT_MOTOR_TEST = 0XCC,       //�?达测�?

    //恢�?�出厂�?�置
    APP_CMD_FACTORY_RECOVER = 0xFF, //手环恢�?�出厂�?�置
} cmd_t;
typedef enum __ecg_ppg_lead_state_t
{
    ecg_ppg_lead_state_invalid,
    ecg_ppg_lead_state_on,
    ecg_ppg_lead_state_off,
} ecg_ppg_lead_state_t;

typedef __packed struct
{
    uint8_t key;
    uint8_t data[14];
    uint8_t crc;
} cmd_packet_t;

typedef void (*cmd_handler_t)(cmd_packet_t *packet);

typedef __packed struct
{
    uint8_t key;
    cmd_handler_t handler;
} cmd_entry_t;

void ProtocolRxDataHandler(ble_nus_t *, uint8_t *, uint16_t);
void ProtocolTxDataHandler(uint8_t *, uint16_t);

uint16_t calc_checksum(uint8_t *sdata, uint32_t len); // crc校验函数
void BraceletSendToAPPCamera(uint8_t CMD);
void APPhealth_Init(void);
void APPhealth_Stop(void);
void APPSetTime_Start(void);

void AsynchronousSendTime_Init(void);
void Look_for_band_timer_start(void);

void APPhealthGetdata_Init(void);
void SportDetailTime_Init(void);
void SleepDetailTime_Init(void);

void Get_ancs_Init(void);

void MoveHandInit(void);
void DistrubInit(void);

// add by mlw at 20161216 20:18
bool m_dont_disturb_section_check(void);
void m_dont_disturb_section_calibration(uint32_t seconds);

void app_cmd_bp_am_get_data(cmd_packet_t *packet);
// void m_anti_lost_switch_get(uint8_t* ctrlBuf);
// void AntiLostInit(void);

//增加安卓手机蓝牙呼吸包功�?
void ble_disconnected(void);
bool ble_if_connected(void);
uint16_t ble_get_conn_handle(void);
//
void wdt_timer_feed(void);
uint8_t app_health_test_get_type(void);
uint8_t app_realtime_hr_test_get_state(void);
uint8_t app_realtime_hr_test_get_hr(void);
// uint16_t app_health_test_get_result(void);
extern uint8_t app_health_test_result[2];
extern uint8_t APPhealth_Type;
uint8_t app_health_gt_test_timer_count(void);

void system_into_poweroff(void);
void power_on(void);
void APPModule_Stop(void);
phone_system_type_t get_phone_system_type(void);
void set_phont_system_type(phone_system_type_t type);

void app_cmd_send_data(uint8_t key, uint8_t *p_data, uint16_t len);
void app_cmd_send_ack(uint8_t key, uint8_t b_fail);
void app_cmd_send_data_byte(uint8_t key, uint8_t data);

void ble_cmd_drv_handle(void);
void ble_cmd_rx_to_buff(uint8_t *p_data);
void T90BLE_Start(void);
void T90BLE_Stop(void);
typedef enum __sports_save_state_t
{
    sports_save_state_unknown,
    sports_save_state_save,
    sports_save_state_unsave,
} sports_save_state_t;
void sports_state_change(uint32_t save_state);
void blecom_process(void);
#endif
